package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JasonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService ;
    @Autowired
    private IStrategyService strategyService ;

    /*
    * 获取攻略文章的内容
    *
    * */
    @RequestMapping(value = "/getContent.do",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String getContent(Long id){
        String content = strategyDetailService.getContentByDetailId(id);
        return content ;
    }


    @RequestMapping("/saveOrUpdate.do")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail, MultipartFile img){
        JasonResult jr = new JasonResult();
        //需要判断一下对方有没有上传封面的图片，有的话就将上传文件所得到的路径交给对象中的封面属性
        if (img!=null&&img.getContentType().startsWith("image")&&img.getSize()>0) {
            String url = UploadUtil.upload(img);
            strategyDetail.setCoverUrl(url);
        }
        //执行新增或者修改的语句
        try {
            strategyDetailService.saveOrUpdate(strategyDetail);
        } catch (Exception e) {
            e.printStackTrace();
            jr.mark(e.getMessage());
        }
        return jr ;
    }


    /**
     * 攻略文章管理的主页面
     */
    @RequestMapping("/list.do")
    public String list(Model model, @ModelAttribute("qo") StrategyQueryObject strategyQueryObject){
        //设置排序的方式,发布的时间为倒序
        strategyQueryObject.setOrderBy("sd.releaseTime desc");
        model.addAttribute("pageInfo",strategyDetailService.query(strategyQueryObject));
        //查询所有没有被禁用的大攻略
        model.addAttribute("strategys",strategyService.listEnable());
        return "strategyDetail/list";
    }



}
